package com.example.mq.mqserver.core;

import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

@Data
public class Message implements Serializable {
    private BasicProperties basicProperties = new BasicProperties((byte)2);//  默认持久化
    private byte[] body;

    //  辅助属性
    //  持久化消息时的偏移量
    //  被 transient 修饰的变量不会参与序列化，并且 @Data 自带的 equals() 不会比较该字段
    private transient long offsetBeg = 0;
    private transient long offsetEnd = 0;
    //  消息是否有效 0x1:有效 0x0:无效
    //  因为消息要参加序列化，所以尽量使用字节来表示数据
    private byte isValid = 0x1;

    private Message(){}
    //  创建工厂方法创建消息
    public static Message createMessageWithId(BasicProperties basicProperties,String routingKey,byte[] body) {
        Message message = new Message();
        if(basicProperties != null) {
            message.basicProperties = basicProperties;
        }
        message.setMessageId("M-" + UUID.randomUUID());
        //  routingKey 冲突，以单独的为主
        message.setRoutingKey(routingKey);
        message.setBody(body);
        return message;
    }

    public String getMessageId() {
        return basicProperties.getMessageId();
    }
    private void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }
    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }
    private void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }
    public byte getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

}
